package com.example.base_kotlin_xml

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter4.util.addOnDebouncedChildClick
import com.example.base_kotlin_xml.adapter.MainListAdapter
import com.example.base_kotlin_xml.databinding.FragmentHomeBinding
import com.example.base_kotlin_xml.view.CustomActivity
import com.example.base_model.bean.general.UserInfo
import com.example.base_model.general.GlobalStateManager
import com.example.base_model.utils.FlowEventBus
import com.example.base_model.view.BaseFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import javax.inject.Inject

/**
 * @Author: jiangKunKun
 * @CreateDate: 2025/11/21
 * @Description:
 * @SpittingGrooves:
 */
@AndroidEntryPoint
class HomeFragment : BaseFragment<FragmentHomeBinding>() {
    val adapter = MainListAdapter()

    @Inject
    lateinit var globalState: GlobalStateManager
    override fun attachLayoutRes(): Int = R.layout.fragment_home

    override fun beforeSetContent() {
        super.beforeSetContent()
    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
    }

    override fun initClickEvents() {

    }

    override fun initData() {
        val associateWith = resources.getStringArray(R.array.main_list).toList()
        mBinding.rvList.layoutManager = LinearLayoutManager(activity)
        // 适配器
        adapter.submitList(associateWith)
        // 子view 防抖点击事件
        adapter.addOnDebouncedChildClick(R.id.btn_main_item, 500){ adapter, view, position ->
            when(associateWith[position]){
                "网络请求" -> {
                    val userInfo = UserInfo(
                        name = "jkk123",
                        age = 18,
                        email = "jkk@163.com",
                        phone = "12345678901",
                        id = 123,
                        isVip = true
                    )
                    val actionHomeFragmentToNetworkFragment =
                        HomeFragmentDirections.actionHomeFragmentToNetworkFragment(
                            userId = 123,
                            userName = "jkk",
                            isActive = true,
                            userInfo = userInfo,
                            friendList = arrayOf(userInfo, userInfo, userInfo),
                            tags = arrayOf("tag1", "tag2", "tag3"),
                            scores = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                        )
                    navigate(actionHomeFragmentToNetworkFragment)
                }
                "事件总线FlowEventBus" -> {
                    FlowEventBus.postStickyEvent("粘性事件", "从mian发送的粘性事件")
                    navigate(R.id.action_homeFragment_to_flowEventBusFragment)
                }
                "全局状态ChangeState" -> {
                    navigate(R.id.action_homeFragment_to_globalStateFragment)
                }
                "moshi解析" -> {
                    navigate(R.id.action_homeFragment_to_moshiFragment)
                }
                "LoopTimer" -> {
                    navigate(R.id.action_homeFragment_to_loopTimerFragment)
                }
                "数据库room" -> {
                    navigate(R.id.action_homeFragment_to_roomFragment)
                }
                "按钮防抖和最后一次点击" -> {
                    navigate(R.id.action_homeFragment_to_antiShakeFragment)
                }
                "datastore数据存储(SP)" -> {
                    navigate(R.id.action_homeFragment_to_datastoreFragment)
                }
                "多view点击监听" -> {
                    navigate(R.id.action_homeFragment_to_multiViewClickFragment)
                }
                "消息队列处理" -> {
                    navigate(R.id.action_homeFragment_to_flowMessageQueueFragment)
                }
                "udp和tcp" -> {
                    navigate(R.id.action_homeFragment_to_udpAndTcpFragment)
                }
                "心跳监听" -> {
                    navigate(R.id.action_homeFragment_to_heartbeatMonitorFragment)
                }
                "mqtt" -> {
                    navigate(R.id.action_homeFragment_to_mqttFragment)
                }
                "自定义View" -> {
                    goActivity(CustomActivity::class.java)
                }
            }
        }
        mBinding.rvList.adapter = adapter
    }

    override fun initObservers() {
        FlowEventBus.observeEvent<String>(lifecycleScope, "123"){
            LogUtils.d("MainActivity 监听其他页面发送的普通事件", it)
        }
        launchAndCollect{
            globalState.appTitle.state.collectLatest {
                LogUtils.d("MainActivity 页面监听appTitle状态变化", it)
            }
        }

        // 监听来自 NetworkFragment 的结果
        parentFragmentManager.setFragmentResultListener("from_network_fragment", viewLifecycleOwner) { _, bundle ->
            val result = bundle.getString("network_result")
            val userId = bundle.getInt("user_id")
            LogUtils.d("接收到NetworkFragment返回结果: $result, userId: $userId")

        }
    }

    override fun onBackPress() {

    }
}